#!/bin/sh
#
# image/iso.sh: ISO image functions.
#
# $Id: iso.sh.in 575 2008-08-26 12:07:20Z enki $
# -------------------------------------------------------------------------
test $lib_archive_iso_sh || {

# -------------------------------------------------------------------------
: ${prefix:="@prefix@"}
: ${exec_prefix:="@prefix@"}
: ${libdir:="${exec_prefix}/lib"}
: ${shlibdir:="${libdir}/sh"}

# -------------------------------------------------------------------------
. $shlibdir/util.sh
. $shlibdir/std/var.sh
. $shlibdir/std/escape.sh

# iso_cmd [options] <iso-image> [files/directories...]
#
# Outputs a suitable command-line for the iso program.
#
#     -l, --level NUM         Compression level: 0 = store -> 9 = best.
#     -d, --dereference       Dump the files instead of symlink pointing to.
#     -x, --exclude PATTERN   Exclude files matching PATTERN.
# -------------------------------------------------------------------------
iso_cmd()
{
  local cmd= s=' ' c=false IFS="$nl" args filter
  eval "set -- $(getopt --name=iso --shell=sh \
                        --longoptions=level:,dereference,exclude:,no-recursion,create,delete,list,unpack,lowercase -o 0123456789hx:RcdluL -- "$@")"

  cmd="isoinfo"
  unset args filter

  
  while [ -n "$1" ]; do
    case $1 in
      # compression level
      -[0-9]|--level)

         # remove any previous setting
         cmd=${cmd/-[0-9]/$s}
         cmd=${cmd%$s-[0-9]}

         case $1 in
           -[0-9])  cmd=${cmd:+$cmd$s}$1 ;;
           --level) cmd=${cmd:+$cmd$s}-$(($2 % 10)) && shift ;;
         esac
         ;;

      # transform (some) names to lowercase
      -L|--lowercase)
         cmd=${cmd:+$cmd$s}'-L'
         ;;

      # exclude files/dirs...
      -x|--exclude)

         xcl=${xcl:+$xcl$s}'-x'$s"\"`escape_dquote "$2"`\""
         xcl=${xcl:+$xcl$s}'-x'$s"\"*/`escape_dquote "$2"`/*\""
         xcl=${xcl:+$xcl$s}'-x'$s"\"*/`escape_dquote "$2"`\""
         shift
         ;;

      -l|--list)       args="${args+$args$s}-f"; filter="|sed 's,^/,, ; s|;[0-9]$||'" ;;
      -X|--unpack)       cmd="7z${s}x${s}" ;;
      --) shift && break ;;
    esac
    shift
  done
  cmd="$cmd${s}-i${s}\"`escape_dquote "$1"`\"${s}${args}${filter}"
  echo "$cmd"
}

# iso_create <iso-image> [files/directories...]
#
# Pack the specified files and directories into a newly created iso-image.
# -------------------------------------------------------------------------
iso_create()
{
  local IFS= a=${1:?image name required}
  msg "Creating ISO-image '$1'..."
  eval `iso_cmd --create "$@"`
}

# iso_update <iso-image> [files/directories]
#
# Update the iso-image (adding when necessary) with the specified files
# and directories.
# -------------------------------------------------------------------------
iso_update()
{ 
  local IFS= a=${1:?image name required}
  msg "Creating ISO-image '$1'..."
  eval `iso_cmd --update "$@"`  
}

# iso_list <iso-image> [files/directories]
#
# List either all or only the specified files and directories from the iso-image.
# -------------------------------------------------------------------------
iso_list()
{ 
  local IFS= a=${1:?image name required}
  #msg "Listing contents of ISO-image '$1'..."
  eval `iso_cmd --list "$@"`  
}

# -------------------------------------------------------------------------
iso_unpack()
{
  uniso -a -o ${2:+-d"$2"} "$1" 2>&1 | sed "1d
    s,^\s*[^:]\+: ,,
    s,\s*\[.*\]\s*\$,,"
}

# --- eof ---------------------------------------------------------------------
lib_archive_iso_sh=:;}
